/**
 * Copyright 2015 Software innovation and R & D center. All rights reserved.
 * File Name: SysUserDao.java
 * Encoding UTF-8
 * Version: 0.0.1
 * History:	2015年1月20日
 */
package com.sirdc.modules.sys.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.filter.SysUserFilter;
import com.sirdc.modules.utils.CollectionsUtils;
import com.sirdc.modules.utils.StringUtils;

/**
 * 
 * @version Revision: 0.0.1
 * @Date: 2015年1月20日
 */
@Repository
public class SysUserDao extends StringDao<SysUser> {

	public List<SysUser> query(final SysUserFilter filter) {
		GenericQuery query = createQuery("obj");
		if (StringUtils.isNotBlank(filter.getName())) {
			query.append(" and obj.name like :name");
			query.setParam("name", StringUtils.getLikewords(filter.getName()));
		}
		
		if (CollectionsUtils.isNotEmpty(filter.getInIds())) {
			query.append(" and obj.sysId in (:ins)");
			query.setParamList("ins", filter.getInIds());
		}
		
		if (CollectionsUtils.isNotEmpty(filter.getNotInIds())) {
			query.append(" and obj.sysId not in (:notins)");
			query.setParamList("notins", filter.getNotInIds());
		}
		
		if (StringUtils.isNotBlank(filter.getTeachClass())) {
			query.append(" and obj.teachClass = :teachClass");
			query.setParam("teachClass", filter.getTeachClass());
		}
		
		//如果不是超级管理员，则不显示超级管理员用户
		if(!filter.isSuperAdmin()) {
			query.append(" and obj.superAdmin = :superAdmin");
			query.setParam("superAdmin", false);
		}
		return query.listResult(filter);
	}

	/**
	 * 批量删除用户(帐号停用)
	 * @author: weihuang.peng
	 * @param ids
	 */
	public void batchDeleteUser(List<String> ids) {
		if (CollectionsUtils.isNotEmpty(ids)) {
			GenericQuery query = create("update SysUser user set user.state=:state").setParam("state", "03");
			query.append(" where user.id in (:ids)").setParamList("ids", ids);
			query.append(" and user.superAdmin != :superAdmin").setParam("superAdmin", true);
			query.executeUpdate();
		}
	}

	/**
	 * 统计一个教学班的人数
	 * @author: huiyang.yu
	 * @param teachClass
	 * @return
	 */
	public Integer queryCountByTeachClass(String teachClass) {
		GenericQuery query = createQuery("obj");
		query.append(" and obj.teachClass =:teachClass").setParam("teachClass", teachClass);
		return query.count();
	}

	public List<SysUser> getByInName(String[] names) {
		GenericQuery query = createQuery("obj");
		query.append(" and obj.name in :(names)").setParam("names", names);
		return query.listResult();
	}

}
